package cn.kgc.bilibili.controller;

import cn.kgc.bilibili.pojo.Danmu;
import cn.kgc.bilibili.service.DanmuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class TestController {


    @Resource
    DanmuService danmuService;


    @ResponseBody
    @GetMapping("/getdanmu")
    public String findDanmus(@RequestParam(value = "measureId",required = true) Integer measureId) {
        System.out.println(measureId);
        String jsonString = "[";
        List<Danmu> danmuList = danmuService.getDanmuByMeasureId(measureId);
        if (danmuList.size() != 0 && danmuList != null) {
            if (danmuList.size() == 1) {
                jsonString = jsonString + "\'{\"text\":\""
                        + danmuList.get(0).getText() + "\",\"color\":\""
                        + danmuList.get(0).getColor() + "\",\"size\":\""
                        + danmuList.get(0).getSize() + "\",\"postion\":\""
                        + danmuList.get(0).getPosition() + "\",\"time\":"
                        + danmuList.get(0).getTime() + "}\']";
            } else {
                for (int i = 0, len = danmuList.size(); i < len; i++) {
                    if (i == len - 1) {
                        jsonString = jsonString + "\'{\"text\":\""
                                + danmuList.get(i).getText() + "\",\"color\":\""
                                + danmuList.get(i).getColor() + "\",\"size\":\""
                                + danmuList.get(i).getSize() + "\",\"postion\":\""
                                + danmuList.get(i).getPosition() + "\",\"time\":"
                                + danmuList.get(i).getTime() + "}\']";
                    } else {
                        if (i == 0) {
                            jsonString = jsonString + "\'{\"text\":\"" +
                                    danmuList.get(i).getText()
                                    + "\",\"color\":\"" +
                                    danmuList.get(i).getColor()
                                    + "\",\"size\":\""
                                    + danmuList.get(i).getSize()
                                    + "\",\"position\":\""
                                    + danmuList.get(i).getPosition()
                                    + "\",\"time\":"
                                    + danmuList.get(i).getTime() + "}\',";
                        } else {
                            jsonString = jsonString + "\'{\"text\":\"" +
                                    danmuList.get(i).getText()
                                    + "\",\"color\":\"" +
                                    danmuList.get(i).getColor()
                                    + "\",\"size\":\""
                                    + danmuList.get(i).getSize()
                                    + "\",\"position\":\""
                                    + danmuList.get(i).getPosition()
                                    + "\",\"time\":"
                                    + danmuList.get(i).getTime() + "}\',";
                        }
                    }
                }
            }
        }
        return jsonString;
    }

    @ResponseBody
    @PostMapping(value = "addDanmu")
    public String addDanmu(String danmu,@RequestParam(value = "measureId",required = true) Integer measureId) {
        System.out.println(danmu);
        System.out.println("measureId:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+measureId);
        String d[] = danmu.toString().split(",");
        String text = d[0].split(":")[1].substring(1, d[0].split(":")[1].length() - 1);
        System.out.println(text);
        String color = d[1].split(":")[1].substring(1, d[1].split(":")[1].length() - 1);
        System.out.println(color);
        String size = d[2].split(":")[1].substring(1, d[2].split(":")[1].length() - 1);
        System.out.println(size);
        String position = d[3].split(":")[1].substring(1, d[3].split(":")[1].length() - 1);
        System.out.println(position);
        int time = Integer.parseInt(d[4].split(":")[1].substring(0, d[4].split(":")[1].length()-1));
        System.out.println(time);
        Danmu danmuNew = new Danmu(measureId, text, color, size, position, time);
        int status = danmuService.addDanmu(danmuNew);
        if (status > 0) {
            return "添加成功!!";
        } else {
            return "添加失败!!";
        }

    }

    @PostMapping("trytry")
    public void addDanmus(String danmu) {
        System.out.println(danmu);

    }

   /* @RequestMapping(value = "postDanmu", method = RequestMethod.POST)
    @ResponseBody
    public void postDanmu(String[] danmu, String data, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        String danmus = Arrays.toString(danmu);
        System.out.println(data);
        try {
            request.setCharacterEncoding("utf-8");

            response.setContentType("text/html;charset=utf-8");
            JSONArray jsonArray = new JSONArray(danmus);
            for (int i = 0; i < jsonArray.length(); i++) {
                org.json.JSONObject jsonObject = jsonArray.getJSONObject(i);//解析json
                String text = jsonObject.getString("text");//下边是把json中的信息拿出来放到你的信息中
                String color = jsonObject.getString("color");
                Integer position = Integer.parseInt(jsonObject.getString("position"));
                Integer size = Integer.parseInt(jsonObject.getString("size"));
                Integer time = Integer.parseInt(jsonObject.getString("time"));
                Danmu danmu1 = new Danmu(1, text, color, position, size, time);//这是我的domain
                danmuService.addDanmu(danmu1);
            }
        } catch (JSON e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }*/


}
